import cv2
import numpy as np
import os

# 定义输入和输出文件夹路径
input_folder = '/hy-tmp/datas/images/test/pos'
output_folder = '/hy-tmp/cali'

# 确保输出文件夹存在
os.makedirs(output_folder, exist_ok=True)

# 获取输入文件夹中的所有图片文件
image_files = [f for f in os.listdir(input_folder) if os.path.isfile(os.path.join(input_folder, f))]

# 处理每个图片文件
for idx, image_file in enumerate(image_files, start=1):
    # 读取图片
    image_path = os.path.join(input_folder, image_file)
    image = cv2.imread(image_path)

    # 检查图片是否成功读取
    if image is None:
        print(f"Failed to read {image_path}")
        continue

    # 调整图片大小为 224x224
    resized_image = cv2.resize(image, (224, 224))

    # 转换为 3x224x224 的形状
    resized_image = resized_image.transpose((2, 0, 1))

    # 转换为 NumPy 数组
    image_array = np.array(resized_image, dtype=np.float32) / 255.0
    # print(image_array)

    # 写入 .idx.float 文件
    output_path = os.path.join(output_folder, f'pos_{idx}.float')
    image_array.tofile(output_path)

    print(f"Processed {image_path} -> {output_path}")